package com.jiemengshi.server.service;

import com.jiemengshi.server.mapper.UserAdminMapper;
import com.jiemengshi.server.mapper.UserWechatMapper;
import com.jiemengshi.server.model.UserAdmin;
import com.jiemengshi.server.model.UserWechat;
import com.jiemengshi.server.util.CommonUtil;
import com.jiemengshi.server.util.DateUtil;
import com.jiemengshi.server.util.ErrorCodeMsg;
import com.jiemengshi.server.util.PrjException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by MingQi on 2017/10/14.
 */
@Service
public class AdminUserService {
    @Autowired
    private UserAdminMapper userAdminMapper;

    @Autowired
    private UserWechatMapper userWechatMapper;
    
    public boolean addUserAdmin(UserAdmin admin) {
        admin.setCreateTime(DateUtil.getCurrentTime());
        admin.setId(null);
        admin.setLastLogin(DateUtil.getCurrentTime());
        admin.setPassword(CommonUtil.MD5(admin.getPassword()));

        return userAdminMapper.insertSelective(admin) > 0;
    }

    public boolean deleteUserAdminByUsername(String username) {
        boolean success = userAdminMapper.deleteByUsername(username) > 0;
        return success;
    }
    
    public UserAdmin userAdminLogin(HttpServletRequest request, String username, String password) throws PrjException {
        password = CommonUtil.MD5(password);

        UserAdmin admin = userAdminMapper.selectByUsernameAndPassword(username, password);
        if (admin != null) {
            request.getSession().setAttribute("adminInfo", admin.getId() + "_" + admin.getPermissionLevel());
            updateUserLoginTime(admin.getId());
            admin.setLastLogin(DateUtil.getCurrentTime());
            return admin;
        } else {
            throw new PrjException(ErrorCodeMsg.USER_ADMIN_LOGIN_FAIL);
        }
    }

    public int getLoginUserId(HttpServletRequest request) throws PrjException {
        Object sAdminInfo = request.getSession().getAttribute("adminInfo");

        if(sAdminInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_ADMIN_NOT_LOGIN);

        return Integer.parseInt(((String) sAdminInfo).split("_")[0]);
    }

    public int getLoginUserPermissionLevel(HttpServletRequest request) throws PrjException {
        Object sAdminInfo = request.getSession().getAttribute("adminInfo");

        if(sAdminInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_ADMIN_NOT_LOGIN);

        return Integer.parseInt(((String) sAdminInfo).split("_")[1]);
    }

    public boolean updateUserLoginTime(int userId) {
        return userAdminMapper.updateLoginTime(userId, DateUtil.getCurrentTime()) > 0;
    }
    
    public boolean userAdminLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("adminInfo");
        return true;
    }

    public List<UserWechat> getUserWechatList(int start, int limit) {
        return userWechatMapper.listUser(start * limit, limit);
    }

    public boolean updateUserWechatLevel(int userId, int level) {
        return userWechatMapper.updateLevel(userId, level) > 0;
    }
}
